﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Pasys.PresentationCore;

namespace Pasys.Document
{
    public partial class SystemSetting : Form
    {
        private string defaultKey = Guid.NewGuid().ToString();

        public SystemSettingManager SystemSettings { get; set; }
        public SystemSetting()
        {
            InitializeComponent();
        }

        private void SystemSetting_Load(object sender, EventArgs e)
        {
            AddSubProviders(null, null);
        }

        private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            if (e.Node.Nodes.Count == 1 && e.Node.Nodes[0].Text == defaultKey)
            {
                e.Node.Nodes.Clear();
                var provider = e.Node.Tag as ISettingViewProvider;
                AddSubProviders(e.Node, provider);
            }
        }

        private void AddSubProviders(TreeNode parentNode, ISettingViewProvider provider)
        {
            var roots = SystemSettings.GetSettingViewProviders(provider);
            foreach (var svp in roots)
            {
                TreeNode node = null;
                if (parentNode == null)
                {
                    node = this.treeView1.Nodes.Add(svp.SettingKey, svp.Title);
                }
                else
                {
                    node = parentNode.Nodes.Add(svp.SettingKey, svp.Title);
                }
                node.Tag = svp;
                if (svp.IsPath)
                {
                    node.Nodes.Add(defaultKey);
                }
            }
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            panel1.Controls.Clear();
            var provider = e.Node.Tag as ISettingViewProvider;
            if (provider.IsPath)
            {
                return;
            }

            var c = provider.View;
            var f = c as Form;
            if (f != null)
            {
                f.TopLevel = false;
                f.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
                //f.Dock = DockStyle.Fill;
                f.Visible = true;
            }

            //c.Width = panel1.Width;
            //c.Height = panel1.Height;
            panel1.Controls.Add(c);
        }
    }
}
